import { CopyControllerBase } from "./CopyControllerBase";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { DataEvent } from "../../../events/DataEvent";
import { CrossSeanHintBtn } from "../crossSea/CrossSeanHintBtn";
import { SCrossSeaList } from "../../../../../Message/Public/SCrossSeaList";
import { SCrossSeaInfo } from "../../../../../Message/Public/SCrossSeaInfo";
import { ETaskGroup } from "../../../../../Message/Game/ETaskGroup";
import { ETaskStatus } from "../../../../../Message/Game/ETaskStatus";
import { GameManager } from "../../../manager/GameManager";
import { ENpcEffect } from "../../../../../Message/Public/ENpcEffect";
import { CrossSeaEnterWin } from "../crossSea/CrossSeaEnterWin";
import { GameProxy } from "../../../mvc/GameProxy";
//class CrossSeaController
    
    export  class CrossSeaController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSeaOpen, this.openHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSeaClose, this.closeHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossSeaList, this.romGotHandler);
            Dispatcher.addEventListener(EventName.CrossSeaClickHintBtn, this.clickHintBtnHandler);
            Dispatcher.addEventListener(EventName.CrossSeaShowEnterWinReq, this.showEnterWinHandler);
            Dispatcher.addEventListener(EventName.CrossSeaEnterSeaReq, this.enterSeaReqHandler);
            NetDispatcher.addCmdListener(ServerCommand.FairylandOpenCommand, this.bfOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.FairylandCloseCommand, this.bfCloseHandler);
            return;
        }

        protected /* override */ addListener(): void
        {
            super.addListener();
            return;
        }

        protected /* override */ removeListener(): void
        {
            super.removeListener();
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            if (!(this._enterWin == null) && !this._enterWin.isHide) 
            {
                this._enterWin.hide();
            }
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            return;
        }

        /* internal  */openHandler(arg1: MessageBlock): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryBuffSkillOpened));
            if (this._bfOpening) 
            {
                return;
            }
            this.isOpening = true;
            if (this._icon == null) 
            {
                this._icon = new CrossSeanHintBtn();
                this._icon.addEventListener(flash.events.MouseEvent.CLICK, this.clickIconHandler);
            }
            this._icon.showcrossSeaEffect(true);
            this._icon.show();
            return;
        }

        /* internal  */bfOpenHandler(arg1: Object): void
        {
            this._bfOpening = true;
            this.isOpening = false;
            if (!(this._icon == null) && !this._icon.isHide) 
            {
                this._icon.hide();
            }
            return;
        }

        /* internal  */bfCloseHandler(arg1: Object): void
        {
            this._bfOpening = false;
            return;
        }

        /* internal  */clickIconHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._icon == null) 
            {
                return;
            }
            this._icon.showcrossSeaEffect(false);
            return;
        }

        /* internal  */closeHandler(arg1: MessageBlock): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.CountryBuffSkillClose));
            this.isOpening = false;
            if (!(this._icon == null) && !this._icon.isHide) 
            {
                this._icon.hide();
            }
            return;
        }

        /* internal  */romGotHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SCrossSeaList;
            var loc2=loc1.crossSeas;
            var loc3;
            if ((loc3 = loc2[0] as SCrossSeaInfo) == null) 
            {
                return;
            }
            if (this._enterWin == null) 
            {
                return;
            }
            this._enterWin.data = loc2;
            return;
        }

        /* internal  */clickHintBtnHandler(arg1: DataEvent): void
        {
            var loc3=false;
            var loc4=false;
            var loc1=cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupFairyland, true, ETaskStatus._ETaskStatusHadCompleted);
            var loc2=cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupCrossSea, true, ETaskStatus._ETaskStatusHadCompleted);
            if (!loc1 || !loc2) 
            {
                loc3 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupFairyland, false, -1);
                loc4 = cache.task.hasTaskByGroup(ETaskGroup._ETaskGroupCrossSea, false, -1);
                if (loc3 || loc4) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FairylandOpenTaskPanel));
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FairylandCloseTaskPanel));
                    if (this.isOpening) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.CrossSeaShowEnterWinReq));
                    }
                    else 
                    {
                        GameManager.instance.popupWindow(ENpcEffect._ENpcEffectCopy129);
                    }
                }
            }
            return;
        }

        public showEnterWinHandler(arg1: DataEvent): void
        {
            if (!(this._enterWin == null) && !this._enterWin.isHide) 
            {
                this._enterWin.hide();
                return;
            }
            if (this._enterWin == null) 
            {
                this._enterWin = new CrossSeaEnterWin();
            }
            this._enterWin.show();
            GameProxy.copy.getCrossSeaList();
            return;
        }

        /* internal  */enterSeaReqHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SCrossSeaInfo;
            if (loc1 == null || loc1.seaId == null) 
            {
                return;
            }
            GameProxy.copy.enterCrossSea(loc1.seaId);
            return;
        }

        /* internal */ /* var */_icon: CrossSeanHintBtn;

        /* internal */ /* var */_enterWin: CrossSeaEnterWin;

        public /* var */isOpening: Boolean=false;

        public /* var */_bfOpening: Boolean=false;
    }
